import React from 'react';
import _ from 'lodash';
import { TextWithTags, utils, NumberView } from '@/components/BaseTable';
import { Statistic, Tooltip } from 'antd';
import styles from '../index.less';

export const hasChildren = (list, item) => {
  let result = false;
  list.forEach(value => {
    if (value.pid === item.id) {
      result = true;
    }
  });
  return result;
};
export const buildTree = (list, pid = '0') => {
  const tree = [];
  list.forEach(value => {
    const x = value;
    if (value.pid === pid) {
      if (hasChildren(list, value)) {
        x.children = [];
        x.children = buildTree(list, value.id);
      }
      tree.push(x);
    }
  });
  return tree;
};
export const getExpandedRowKeys = list => {
  const expandedRowKeys = [];
  _.forEach(list, value => {
    expandedRowKeys.push(value.id);
  });
  return expandedRowKeys;
};

export const targetTypes = [
  {
    key: 'investment',
    name: '投资版',
  },
  {
    key: 'strategy',
    name: '战规版',
  },
  {
    key: 'group',
    name: '集团审批版',
  },
  {
    key: 'before',
    name: '调整前',
  },
  {
    key: 'after',
    name: '调整后',
  },
];
export const titleTree = [
  {
    name: '项目/分期/组团/产品/楼栋',
    key: 'id',
    colspan: 1,
    rowspan: 4,
    show: true,
  },
  {
    name: '全盘动态货值',
    key: 'newestAllDynamic',
    rowspan: 1,
    show: true,
    children: [
      {
        name: '总货值(不含未推售车位)',
        key: 'total_price',
        colspan: 4,
      },
      {
        name: '未推售车位货值',
        key: 'dynamic_sale_price',
        colspan: 2,
      },
    ],
  },
  // {
  //   name: '核心指标',
  //   key: 'core_indicators',
  //   rowspan: 1,
  //   show: true,
  //   children: targetTypes.map(item => ({
  //     ...item,
  //     colspan: 6,
  //   })),
  // },
];

export const fixedColumns = () => {
  const arr = [
    {
      key: 'name',
      dataKey: 'name',
      width: 270,
      frozen: 'left',
      dataGetter: ({ rowData }) => {
        const rowDataX = rowData;
        const result = {
          text: rowDataX.name,
        };
        result.textFirst = true;
        if (rowData.type === 'org_build') {
          if (`${rowData.isSupply}` === '0') {
            result.tags = [{ text: '未供', color: 'orange', border: true }];
          }
          if (`${rowData.isSupply}` === '1') {
            result.tags = [{ text: '已供', color: 'green' }];
          }
        }
        return result;
      },
      cellRenderer: <TextWithTags />,
    },
    {
      key: 'num',
      dataKey: 'num',
      align: 'right',
      width: 150,
      headerRenderer: () => (
        <div>
          <div>套数/个数</div>
          <div>(套/个)</div>
        </div>
      ),
      dataGetter: ({ rowData }) => {
        let number = [];
        if (['org_project', 'org_stage', 'org_group'].includes(rowData.type)) {
          number = [rowData.numHouse, rowData.numParking];
        }
        if (`${rowData.isParking}` === '0') {
          number = [rowData.numHouse];
        }
        if (`${rowData.isParking}` === '1') {
          number = [rowData.numParking];
        }
        return utils.calculate.NumberView({
          cellData: number,
          precision: 0,
        });
      },
    },
    {
      key: 'area',
      dataKey: 'area',
      align: 'right',
      width: 150,
      headerRenderer: () => (
        <div>
          <div>面积</div>
          <div>(平米)</div>
        </div>
      ),
      dataGetter: ({ rowData }) => [rowData.saleArea],
      cellRenderer: <NumberView precision={2} />,
    },
    {
      key: 'average_price',
      dataKey: 'average_price',
      align: 'right',
      width: 170,
      headerRenderer: () => (
        <div>
          <div>均价</div>
          <div>(元/平米,元/个)</div>
        </div>
      ),
      dataGetter: ({ rowData }) => {
        let number = [];
        if (['org_project', 'org_stage', 'org_group'].includes(rowData.type)) {
          number = [rowData.averagePriceHouse, rowData.averagePriceParking];
        }
        if (`${rowData.isParking}` === '0') {
          number = [rowData.averagePriceHouse];
        }
        if (`${rowData.isParking}` === '1') {
          number = [rowData.averagePriceParking];
        }
        return utils.calculate.NumberView({
          cellData: number,
          precision: 0,
        });
      },
    },
    {
      key: 'priceValue',
      dataKey: 'priceValue',
      align: 'right',
      width: 210,
      headerRenderer: () => (
        <div>
          <div>金额</div>
          <div>(万元：合计/房源/车位)</div>
        </div>
      ),
      dataGetter: ({ rowData }) => {
        // type: "org_sale_parking" 已售车位
        // type: "org_forsaleparking" 未推售车位
        // type: "org_not_sale_parking" 已定价未售车位
        // type: "org_salingparking" 可售车位
        let number = [];
        if (['org_project', 'org_stage', 'org_group'].includes(rowData.type)) {
          number = [
            rowData.housecontainCommodityValue / 10000 + rowData.nocontainCommodityValue / 10000,
            rowData.housecontainCommodityValue / 10000,
            rowData.nocontainCommodityValue / 10000,
          ];
        }
        if (`${rowData.isParking}` === '0') {
          number = [rowData.housecontainCommodityValue / 10000];
        }
        if (`${rowData.isParking}` === '1') {
          number = [rowData.nocontainCommodityValue / 10000];
        }
        return utils.calculate.NumberView({
          cellData: number,
          precision: 0,
        });
      },
    },
    {
      key: 'forsaleparking_num',
      dataKey: 'forsaleparking_num',
      align: 'right',
      width: 150,
      headerRenderer: () => (
        <div>
          <div>个数</div>
          <div>(个)</div>
        </div>
      ),
      dataGetter: ({ rowData }) => {
        return utils.calculate.NumberView({
          cellData: [rowData.numParkingForsale],
          precision: 0,
        });
      },
    },
    {
      key: 'forsaleparking_value',
      dataKey: 'forsaleparking_value',
      align: 'right',
      width: 150,
      headerRenderer: () => (
        <div>
          <div>金额</div>
          <div>(万元)</div>
        </div>
      ),
      dataGetter: ({ rowData }) =>
        utils.calculate.NumberView({
          cellData: [rowData.commodityValue / 10000],
          precision: 0,
        }),
    },
  ];

  // targetTypes.forEach(item => {
  //   arr.push(
  //     {
  //       key: `${item.key}_equityPrice`,
  //       dataKey: `${item.key}_equityPrice`,
  //       align: 'right',
  //       width: 100,
  //       headerRenderer: () => (
  //         <div>
  //           <div>权益前货值</div>
  //           <div>(万元)</div>
  //         </div>
  //       ),
  //       dataGetter: ({ rowData }) => {
  //         if (rowData[item.key]) {
  //           return utils.calculate.NumberView({
  //             cellData: [rowData[item.key].equityPrice],
  //             precision: 0,
  //           });
  //         }
  //         return '';
  //       },
  //     },
  //     {
  //       key: `${item.key}_equityBeforeProfit`,
  //       dataKey: `${item.key}_equityBeforeProfit`,
  //       align: 'right',
  //       width: 130,
  //       headerRenderer: () => (
  //         <div>
  //           <div>权益前货创造利润额</div>
  //           <div>(万元)</div>
  //         </div>
  //       ),
  //       dataGetter: ({ rowData }) => {
  //         if (rowData[item.key]) {
  //           return utils.calculate.NumberView({
  //             cellData: [rowData[item.key].equityBeforeProfit],
  //             precision: 0,
  //           });
  //         }
  //         return '';
  //       },
  //     },
  //     {
  //       key: `${item.key}_equityAfterProfit`,
  //       dataKey: `${item.key}_equityAfterProfit`,
  //       align: 'right',
  //       width: 130,
  //       headerRenderer: () => (
  //         <div>
  //           <div>权益后货创造利润</div>
  //           <div>(万元)</div>
  //         </div>
  //       ),
  //       dataGetter: ({ rowData }) => {
  //         if (rowData[item.key]) {
  //           return utils.calculate.NumberView({
  //             cellData: [rowData[item.key].equityAfterProfit],
  //             precision: 0,
  //           });
  //         }
  //         return '';
  //       },
  //     },
  //     {
  //       key: `${item.key}_createProfit`,
  //       dataKey: `${item.key}_createProfit`,
  //       align: 'right',
  //       width: 100,
  //       headerRenderer: () => (
  //         <div>
  //           <div>创造利润率</div>
  //           <div>(%)</div>
  //         </div>
  //       ),
  //       dataGetter: ({ rowData }) => ({
  //         text:
  //           rowData[item.key] && rowData[item.key].createProfit
  //             ? `${Number(rowData[item.key].createProfit).toFixed(2)}%`
  //             : '',
  //       }),
  //       cellRenderer: <TextWithTags />,
  //     },
  //     {
  //       key: `${item.key}_meltingIRR`,
  //       dataKey: `${item.key}_meltingIRR`,
  //       align: 'right',
  //       width: 100,
  //       headerRenderer: () => (
  //         <div>
  //           <div>非融IRR</div>
  //           <div>(%)</div>
  //         </div>
  //       ),
  //       dataGetter: ({ rowData }) => ({
  //         text:
  //           rowData[item.key] && rowData[item.key].meltingIRR
  //             ? `${Number(rowData[item.key].meltingIRR).toFixed(2)}%`
  //             : '',
  //       }),
  //       cellRenderer: <TextWithTags />,
  //     },
  //     {
  //       key: `${item.key}_meltingInvestment`,
  //       dataKey: `${item.key}_meltingInvestment`,
  //       align: 'right',
  //       width: 140,
  //       headerRenderer: () => (
  //         <div>
  //           <div>非融静态投资回收期</div>
  //           <div>(月)</div>
  //         </div>
  //       ),
  //       dataGetter: ({ rowData }) => {
  //         if (rowData[item.key]) {
  //           return utils.calculate.NumberView({
  //             cellData: [rowData[item.key].meltingInvestment],
  //             precision: 2,
  //           });
  //         }
  //         return '';
  //       },
  //     },
  //   );
  // });

  return arr;
};

const colRender = (row, colKey) => {
  let text = row[`${colKey}_${row.key}`];
  if (text !== null && text !== undefined && text !== 'null' && text !== 'undefined') {
    text = Number(text).toFixed(row.precision);
  } else {
    text = '-';
  }

  text = text !== '-' ? <Statistic value={text} /> : text;
  return (
    <Tooltip
      title={text}
      trigger="click"
      overlayClassName={styles['core-indicators-modal-tooltip']}
    >
      <div className="ellipsis" style={{ textAlign: 'right' }}>
        {text}
      </div>
    </Tooltip>
  );
};

export const coreIndicatorsColumns = [
  {
    width: 180,
    title: '整盘核心指标',
    dataIndex: 'name',
    key: 'name',
  },
  {
    title: '投资版',
    dataIndex: 'investment',
    key: 'investment',
    render: (text, row) => colRender(row, 'investment'),
  },
  {
    title: '战规版',
    dataIndex: 'strategy',
    key: 'strategy',
    render: (text, row) => colRender(row, 'strategy'),
  },
  {
    title: '集团审批版',
    dataIndex: 'group',
    key: 'group',
    render: (text, row) => colRender(row, 'group'),
  },
  {
    title: '调整前',
    dataIndex: 'before',
    key: 'before',
    render: (text, row) => colRender(row, 'before'),
  },
  {
    title: '调整后',
    dataIndex: 'after',
    key: 'after',
    render: (text, row) => colRender(row, 'after'),
  },
];
